package org.gbif.view.areamap
{
	import com.google.maps.InfoWindowOptions;
	import com.google.maps.LatLng;
	import com.google.maps.MapMouseEvent;
	import com.google.maps.overlays.Marker;
	import com.google.maps.overlays.MarkerOptions;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point;
	
	import org.gbif.view.areamap.infowindows.ImageInfoWindow;
	import org.gbif.view.areamap.infowindows.WikipediaInfoWindow;
	
	public class MarkerCreator
	{
		public static function createFlickrMarker(bitmap:Bitmap,photo:Object):Marker {
			
			var bmd:BitmapData = Bitmap(bitmap).bitmapData;
			var bmd2:BitmapData = new BitmapData(32,32)
			var m:Matrix = new Matrix();
			m.scale(0.43,0.43);
			bmd2.draw(bmd,m);		
			var sp:Sprite= new Sprite();
            sp.graphics.lineStyle(3,0xFF0071);
            sp.graphics.beginFill(0xFFFFFF,0);
            sp.graphics.drawRect(0,0,31,31);
            sp.graphics.endFill();
            bmd2.draw(sp);
			var iconBitmap:Bitmap= new Bitmap(bmd2);
			
			var latlng:LatLng = new LatLng(photo.latitude, photo.longitude);
	      	var photoUrl:String = "http://farm"+photo.farm+".static.flickr.com/"+photo.server+"/"+photo.id+"_"+photo.secret+"_m.jpg";
	      	
	       var marker:Marker = new Marker(latlng, new MarkerOptions(
	       	{tooltip: photo.title,
	       	 draggable:false,
	       	 icon: iconBitmap}));		        
                
	    	var infowindow:ImageInfoWindow= new ImageInfoWindow();
        	infowindow.ownerName=photo.ownername;
        	infowindow.ownerURL="http://www.flickr.com/"+photo.owner;
        	infowindow.title=photo.title;
        	infowindow.photoFileURL=photoUrl;
        	infowindow.photoURL="http://www.flickr.com/photos/"+photo.owner+"/"+photo.id;
        	infowindow.source="flickr";
        	infowindow.photoId="flickr"+photo.id;
	       	
	   		var optionsMark:InfoWindowOptions = new InfoWindowOptions({
	            customContent: infowindow,
	            customOffset: new Point(0, 10),
	            width: 215,
	            drawDefaultFrame: true					
			});  	 
	        marker.addEventListener(MapMouseEvent.CLICK, function(e:MapMouseEvent):void {
	      		marker.openInfoWindow(optionsMark);          
	        });      
	        
	        return marker;			
		} 
		
		public static function createPanoramioMarker(bitmap:Bitmap,photo:Object):Marker {
	      	var bmd:BitmapData = Bitmap(bitmap).bitmapData;
			var bmd2:BitmapData = new BitmapData(32,32)
			bmd2.draw(bmd);		
			var sp:Sprite= new Sprite();
	        sp.graphics.lineStyle(3,0x00A8FF);
	        sp.graphics.beginFill(0xFFFFFF,0);
	        sp.graphics.drawRect(0,0,31,31);
	        sp.graphics.endFill();
	        bmd2.draw(sp);
			var iconBitmap:Bitmap= new Bitmap(bmd2);     
	      	
	      	var latlng:LatLng = new LatLng(photo.latitude, photo.longitude); 	      	
		    var marker:Marker = new Marker(latlng, new MarkerOptions(
		       	{tooltip: photo.photo_title,
		       	 draggable:false,
		       	 icon: iconBitmap}));	
	       	
	    	var infowindow:ImageInfoWindow= new ImageInfoWindow();
	    	infowindow.ownerName=photo.owner_name;
	    	infowindow.ownerURL=photo.owner_url;
	    	infowindow.title=photo.photo_title;
	    	infowindow.photoFileURL=(photo.photo_file_url as String).replace("medium","small");
	    	infowindow.photoURL=photo.photo_url;
	    	infowindow.source="panoramio";
	    	infowindow.photoId="pano"+photo.photo_id;
	       	
	   		var optionsMark:InfoWindowOptions = new InfoWindowOptions({
	            customContent: infowindow,
	            customOffset: new Point(0, 10),
	            width: 215,
	            drawDefaultFrame: true					
			});  	 
	        marker.addEventListener(MapMouseEvent.CLICK, function(e:MapMouseEvent):void {
	      		marker.openInfoWindow(optionsMark);          
	        });	
	        
	        return marker;				
		}		
		
		public static function craeteWikipediaMarker(w:Object,loader:Loader):Marker {
			
			var latlng:LatLng = new LatLng(w.lat, w.lng);		      	
	        var marker:Marker = new Marker(latlng, new MarkerOptions(
			       	{tooltip: w.title,
			       	 draggable:false,
			       	 icon: loader}));	
			
			var infowindow:WikipediaInfoWindow= new WikipediaInfoWindow();	        
				infowindow.title=w.title;
				infowindow.wikipediaUrl=w.wikipediaUrl;
				infowindow.thumbnailImg=w.thumbnailImg;
				
			var html:String = "";
			if (w.thumbnailImg!=null) {html+='<img src="'+w.thumbnailImg+'" />';}
			html+=w.summary; 
				
			infowindow.html=html;					
			var options2:InfoWindowOptions = new InfoWindowOptions({
                customContent: infowindow,
                customOffset: new Point(0, 10),
                width: 397,
                drawDefaultFrame: true					
			});
			marker.addEventListener(MapMouseEvent.CLICK, function(e:MapMouseEvent):void {
				marker.openInfoWindow(options2);
			});
			
			return marker;
		}

	}
}